import { useEffect, useState } from "react"; import { View } from "react-native"; import { router, useLocalSearchParams } from "expo-router"; import { Button } from "@/components/ui/Button"; import CustomSafeAreaView from "@/components/ui/CustomSafeAreaView"; import FullPageSpinner from "@/components/ui/FullPageSpinner"; import { Input } from "@/components/ui/Input"; import { Text } from "@/components/ui/Text"; import { useToast } from "@/components/ui/Toast"; import { useQuery } from "@tanstack/react-query"; import { useEditBookmarkList } from "@karakeep/shared-react/hooks/lists"; import { useTRPC } from "@karakeep/shared-react/trpc"; const EditListPage = () => { const { slug: listId } = useLocalSearchParams<{ slug?: string | string[] }>(); const [text, setText] = useState(""); const [query, setQuery] = useState(""); const { toast } = useToast(); const api = useTRPC(); const { mutate, isPending: editIsPending } = useEditBookmarkList({ onSuccess: () => { dismiss(); }, onError: (error) => { // Extract error message from the error object let errorMessage = "Something went wrong"; if (error.data?.zodError) { errorMessage = Object.values(error.data.zodError.fieldErrors) .flat() .join("\n"); } else if (error.message) { errorMessage = error.message; } toast({ message: errorMessage, variant: "destructive", }); }, }); if (typeof listId !== "string") { throw new Error("Unexpected param type"); } const { data: list, isLoading: fetchIsPending } = useQuery( api.lists.get.queryOptions({ listId, }), ); const dismiss = () => { router.back(); }; useEffect(() => { if (!list) return; setText(list.name ?? ""); setQuery(list.query ?? ""); }, [list?.id, list?.query, list?.name]); const onSubmit = () => { if (!text.trim()) { toast({ message: "List name can't be empty", variant: "destructive" }); return; } if (list?.type === "smart" && !query.trim()) { toast({ message: "Smart lists must have a search query", variant: "destructive", }); return; } mutate({ listId, name: text.trim(), query: list?.type === "smart" ? query.trim() : undefined, }); }; const isPending = fetchIsPending || editIsPending; return ( {isPending ? ( ) : ( {/* List Type Info - not editable */} List Type {/* List Name */} {list?.icon || "🚀"} {/* Smart List Query Input */} {list?.type === "smart" && ( Search Query Smart lists automatically show bookmarks matching your search query )} )} ); }; export default EditListPage;